<?php
/*
 CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`master` CHAR( 20 ) NOT NULL ,
`save_time` INT NOT NULL ,
`class` INT NOT NULL ,
`subject` CHAR( 255 ) NOT NULL ,
`content` TEXT NOT NULL ,
`times` INT NOT NULL 
) ENGINE = MYISAM ;
 */

class news 
{
	static protected $table 	 = 'news';	
	
	static public function add($subject,$content,$master,$class='1')
	{
	   global $db;
	   $bind = array(
	   		'master'=>$master,
	   		'save_time'=>time(),
	   		'class'=>$class,
	   		'subject'=>$subject,
	   		'content'=>$content	   		
	   );
	   return $db->insert(self::$table,$bind);	   
	}
	
	static public function modify($id,$data=array(),$content='',$master='',$save_time='',$class='')
	{
	   global $db;
	   if(!is_array($data))
	   {
	       $data = array(
	       		'subject'=>$data,
	       		'content'=>$content,
	       		'master'=>$master,
	       		'save_time'=>$save_time,
	       		'class'=>$class
	       );
	   }
	   $db->update(self::$table,$data,' id = ? ',(int)$id);
	}
	
	static public function del($id)
	{
		global $db;	
		$db->delete(self::$table,' id = ? ',(int)$id);
	}
	
	static public function ls($begin=0,$lenght=20,$class=0)
	{
	   global $db;
	   $begin = $begin<=1?0:$begin;
	   if($class)
	   {
	    	return $db->dataArray("select `id`,`master`,`save_time`,`class`,`subject`,`times` from `".self::$table."` where `class`=? order by `id` desc limit ".$begin.",".$lenght,$class);
	   }else
	   {
	    	return $db->dataArray("select `id`,`master`,`save_time`,`class`,`subject`,`times` from `".self::$table."` order by `id` desc limit ".$begin.",".$lenght,$class);
	   }	   
	}
	
	static public function get($id)
	{
	 	global $db;
	 	return $db->rsArray("select * from `".self::$table."` where `id`=?  ",(int)$id);
	}
	
	static public function times($id)
	{
	 	global $db;
		$db->adddate(self::$table, array('times'=>1), " `id`=?  ",(int)$id);
	}
	
	static public function get_total($class=0)
	{
	   global $db;
	   if($class)
	   {
	    	$rs = $db->rsArray("select count(id) as cc from `".self::$table."` where `class`=? ",$class);
	   		return $rs['cc'];
	   }else
	   {
	    	$rs = $db->rsArray("select count(id) as cc from `".self::$table."` ");
	    	return $rs['cc'];
	   }
	}	
}
?>